from lxml import etree
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

browser = webdriver.Chrome()
# 设置浏览器窗口大小
browser.set_window_size(1600, 4000)
# 设置隐世等待
wait = WebDriverWait(browser, 5)


def get_page(page):
    if page == 1:
        url = 'https://www.jd.com/'
        browser.get(url)
        # 在wait时间内等待页面定位
        inp = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#key')))
        inp.clear()  # 清除输入框
        inp.send_keys('高达')
        inp.send_keys('\n')
    time.sleep(3)
    # search_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#search button.button')))
    # search_btn.click()

    # 模拟鼠标滚动,加载图片，执行原生js代码
    # window.scrollTo(0, scrollHeight)第一个参数是横向滚动到的坐标点，第二个为纵向
    # 先滚到底部
    str_js = 'var scrollHeight = document.body.scrollHeight;window.scrollTo(0, scrollHeight);'
    browser.execute_script(str_js)

    # 往上滚
    for i in range(16, 0, -1):
        str_js = f'var scrollHeight = document.body.scrollHeight / 16;window.scrollTo(0, scrollHeight * {i});'
        browser.execute_script(str_js)
        time.sleep(2)

    # 保存当前页面内容
    html = browser.page_source

    # 获取节点位置
    page_inp = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#J_bottomPage input.input-txt')))
    # print(page_inp.location)
    str_js = f'window.scrollTo(0,{page_inp.location["y"]});'
    browser.execute_script(str_js)

    # 跳转到下一页
    next_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_bottomPage a.pn-next')))
    next_button.click()
    time.sleep(2)

    return html


def parse_page(html):
    e_html = etree.HTML(html)
    lis = e_html.xpath('//div[@id="J_goodsList"]/ul/li')
    titles = []
    imgs = []
    for li in lis:
        # 标题
        title = li.xpath('.//div[@class="p-name p-name-type-2"]/a/em//text()')
        title = ''.join(title)
        titles.append(title)
        # 图片
        img_url = li.xpath('.//img/@src')
        img_url = img_url[0] if img_url else '无'
        imgs.append(img_url)
    print('title', titles)
    print('imgs', imgs)
    print('img长度：', len(imgs))
    print('title长度：', len(titles))


def main():
    for page in range(1, 5):
        print(page)
        html = get_page(page)
        parse_page(html)


if __name__ == '__main__':
    main()
